using System;
using System.Collections.Generic;
using System.Reflection;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using BoardController.Configs;

namespace BoardController.Views
{
    public partial class IndexWindow : Window
    {
        private ComboBox cmbWindowName;

        List<Type> lisTypes = new List<Type>();

        public IndexWindow()
        {
            InitializeComponent();
            Assembly assembly = Assembly.GetAssembly(this.GetType());

            Type[] types = assembly.GetTypes();

            List<string> lisTypeNames = new List<string>();
            Type baseWindowType = typeof(Window);
            foreach (Type type in types)
            {
                if (baseWindowType.IsAssignableFrom(type))
                {

                    if (type == this.GetType())
                    {
                        continue;
                    }
                    lisTypes.Add(type);
                    Window window = Activator.CreateInstance(type) as Window;

                    lisTypeNames.Add(window.Title);
                }
            }



            //cmbWindowName = this.FindControl<ComboBox>(nameof(cmbWindowName));
            cmbWindowName.DataContext = lisTypeNames;
            cmbWindowName.Items = lisTypeNames;
            if (ProjectConfig.Instance != null)
            {
                if (lisTypeNames.Count - 1 >= ProjectConfig.Instance.LastSelectIndex)
                {
                    cmbWindowName.SelectedIndex = ProjectConfig.Instance.LastSelectIndex;
                }
            }
#if DEBUG
            this.AttachDevTools();
#endif
        }



        private void InitializeComponent()
        {
            AvaloniaXamlLoader.Load(this);
            cmbWindowName = this.FindControl<ComboBox>(nameof(cmbWindowName));
        }

        private void BtnShowWindow_OnClick(object sender, RoutedEventArgs e)
        {
            ProjectConfig projectConfig = ProjectConfig.Instance;
            projectConfig.LastSelectIndex = cmbWindowName.SelectedIndex;
            projectConfig.Save();
            Window window = Activator.CreateInstance(lisTypes[cmbWindowName.SelectedIndex]) as Window;
            window.Show(this);

        }
    }
}
